/<template>
  <div
    class="content"
    style="position: relative;"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      label-position="top"
      label-width="110px"
      class="form"
    >
      <el-form-item
        label="名称："
        prop="name"
      >
        <el-input
          v-model="formData.name"
          clearable
          autocomplete="off"
          placeholder="请输入名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="简称："
        prop="short_name"
      >
        <el-input
          v-model="formData.short_name"
          clearable
          autocomplete="off"
          placeholder="请输入简称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="登录账号："
        prop="account"
      >
        <el-input
          v-model="formData.account"
          clearable
          autocomplete="off"
          placeholder="请输入登录账号"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="密码："
        prop="password"
      >
        <el-input
          v-model="formData.password"
          clearable
          type="password"
          name="AddPassword"
          autocomplete="off"
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
    </el-form>
    <span class="dialog-footer rowFlex">
      <el-button
        class="btn"
        @click="onclose"
      >取 消</el-button>
      <el-button
        class="btn"
        type="primary"
        @click="onsubmit"
      >确 定</el-button>
    </span>
  </div>
</template>

<script>
import { partnerSave } from '@/api/cooperate'
export default {
  props: {
    row: {
      type: Object,
      default: () => {
        return {
          type: []
        }
      }
    }
  },
  data() {
    return {
      input_type: 'text',
      formData: {
        name: '',
        type: [1],
        short_name: '',
        account: '',
        password: ''
      },
      rules: {
        name: [{ required: true, message: '请输入合作方名称', trigger: 'blur' }],
        short_name: [{ required: true, message: '请输入合作方简称', trigger: 'blur' }],
        account: [{ required: true, message: '请输入合作方登录账号', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入合作方登录密码', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: (rule, value, callback) => {
              var passwordreg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/
              if (!passwordreg.test(value)) {
                callback(new Error('密码必须是数字+字母,长度6-10位'))
              } else {
                callback()
              }
            }
          }
        ],
        type: [{ type: 'array', required: true, message: '请至少选择一个角色', trigger: 'change' }]
      }
    }
  },
  mounted() {
    if (this.row.id) {
      this.formData = Object.assign({}, this.row)
    }
  },
  methods: {
    onclose() {
      this.$emit('cancel')
    },
    onsubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.onAdd()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async onAdd() {
      const data = {
        id: this.formData.id,
        account: this.formData.account,
        password: this.formData.password,
        name: this.formData.name,
        short_name: this.formData.short_name,
        type: [1]
      }
      const res = await partnerSave(data)
      if (res.status_code === 1) {
        this.$message.success(res.msg)
        this.$emit('ok')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.el-form--label-top .el-form-item__label {
  padding-bottom: 0 !important;
  font-weight: 500;
}
.content {
  .form {
    padding: 20px;
  }
}
</style>